home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / dl_daemon / command.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  6.0 KB  |  185 lines

  1. import time
  2.  
  3. import random
  4. import socket
  5. import eventloop
  6. import logging
  7.  
  8. DAEMONIC_THREAD_TIMEOUT = 2
  9. # amount of time to wait for daemonic threads to quit.  Right now, the only
  10. # thing we use Daemonic threads for is to send HTTP requests to BitTorrent
  11. # trackers.
  12.  
  13. class Command:
  14.     def __init__(self, daemon, *args, **kws):
  15.         self.id = "cmd%08d" % random.randint(0,99999999)
  16.         self.orig = True
  17.         self.args = args
  18.         self.kws = kws
  19.         self.daemon = daemon
  20.  
  21.     def setDaemon(self, daemon):
  22.         self.daemon = daemon
  23.  
  24.     def send(self, callback=None):
  25.         if self.daemon.shutdown:
  26.             return
  27.         eventloop.addIdle(lambda : self.daemon.send(self, callback), "sending command %s" % repr(self))
  28.  
  29.     def setReturnValue(self, ret):
  30.         self.orig = False
  31.         self.ret = ret
  32.  
  33.     def getReturnValue(self):
  34.         return self.ret
  35.  
  36.     def action(self):
  37.         logging.warning ("no action defined for command %s", self.id)
  38.         #for overrriding
  39.  
  40.     def __getstate__(self):
  41.         out = {"id":self.id, "args":self.args, "kws":self.kws, "orig":self.orig}
  42.         try:
  43.             out["ret"] = self.ret
  44.         except AttributeError:
  45.             pass
  46.         return out
  47.  
  48.     def __setstate__(self, data):
  49.         self.id = data["id"]
  50.         self.kws = data["kws"]
  51.         self.args = data["args"]
  52.         self.orig = data["orig"]
  53.         try:
  54.             self.ret = data["ret"]
  55.         except KeyError:
  56.             pass
  57.  
  58. #############################################################################
  59. #  Downloader to App commands                                               #
  60. #############################################################################
  61. class FindHTTPAuthCommand(Command):
  62.     def action(self):
  63.         import httpauth
  64.         id, args = self.args[0], self.args[1:]
  65.         def callback(authHeader):
  66.             c = GotHTTPAuthCommand(self.daemon, id, authHeader)
  67.             c.send()
  68.         httpauth.findHTTPAuth(callback, *args)
  69.  
  70. class AskForHTTPAuthCommand(Command):
  71.     def action(self):
  72.         import httpauth
  73.         id, args = self.args[0], self.args[1:]
  74.         def callback(authHeader):
  75.             c = GotHTTPAuthCommand(self.daemon, id, authHeader)
  76.             c.send()
  77.         httpauth.askForHTTPAuth(callback, *args)
  78.  
  79. class UpdateDownloadStatus(Command):
  80.     def action(self):
  81.         from downloader import RemoteDownloader
  82.         return RemoteDownloader.updateStatus(*self.args, **self.kws)
  83.  
  84. class BatchUpdateDownloadStatus(Command):
  85.     def action(self):
  86.         from downloader import RemoteDownloader
  87.         for status in self.args[0]:
  88.             RemoteDownloader.updateStatus(status)
  89.  
  90. class DownloaderErrorCommand(Command):
  91.     def action(self):
  92.         import util
  93.         util.failed("In Downloader process", details=self.args[0])
  94.  
  95. class ShutDownResponseCommand(Command):
  96.     def action(self):
  97.         self.daemon.shutdownResponse()
  98.  
  99. #############################################################################
  100. #  App to Downloader commands                                               #
  101. #############################################################################
  102. class InitialConfigCommand(Command):
  103.     def action(self):
  104.         import config
  105.         config.setDictionary(*self.args, **self.kws)
  106.  
  107. class UpdateConfigCommand(Command):
  108.     def action(self):
  109.         import config
  110.         config.updateDictionary(*self.args, **self.kws)
  111.  
  112. class StartNewDownloadCommand(Command):
  113.     def action(self):
  114.         from dl_daemon import download
  115.         return download.startNewDownload(*self.args, **self.kws)
  116.  
  117. class StartDownloadCommand(Command):
  118.     def action(self):
  119.         from dl_daemon import download
  120.         return download.startDownload(*self.args, **self.kws)
  121.  
  122. class PauseDownloadCommand(Command):
  123.     def action(self):
  124.         from dl_daemon import download
  125.         return download.pauseDownload(*self.args, **self.kws)
  126.  
  127. class StopDownloadCommand(Command):
  128.     def action(self):
  129.         from dl_daemon import download
  130.         return download.stopDownload(*self.args, **self.kws)
  131.  
  132. class StopUploadCommand(Command):
  133.     def action(self):
  134.         from dl_daemon import download
  135.         return download.stopUpload(*self.args, **self.kws)
  136.  
  137. class GetDownloadStatusCommand(Command):
  138.     def action(self):
  139.         from dl_daemon import download
  140.         return download.getDownloadStatus(*self.args, **self.kws)
  141.  
  142. class RestoreDownloaderCommand(Command):
  143.     def action(self):
  144.         from dl_daemon import download
  145.         return download.restoreDownloader(*self.args, **self.kws)
  146.  
  147. class MigrateDownloadCommand(Command):
  148.     def action(self):
  149.         from dl_daemon import download
  150.         return download.migrateDownload(*self.args, **self.kws)
  151.  
  152. class GotHTTPAuthCommand(Command):
  153.     def action(self):
  154.         id, authHeader = self.args
  155.         import httpauth 
  156.         # note since we're in the downloader process here, httpauth is
  157.         # dl_daemon/private/httpauth.py
  158.         httpauth.handleHTTPAuthResponse(id, authHeader)
  159.  
  160. class ShutDownCommand(Command):
  161.     def response_sent(self):
  162.         import eventloop
  163.         eventloop.quit()
  164.         logging.info ("Shutdown complete")
  165.  
  166.     def action(self):
  167.         starttime = time.time()
  168.         from dl_daemon import download
  169.         download.shutDown()
  170.         import threading
  171.         eventloop.threadPoolQuit()
  172.         for thread in threading.enumerate():
  173.             if thread != threading.currentThread() and not thread.isDaemon():
  174.                 thread.join()
  175.         endtime = starttime + DAEMONIC_THREAD_TIMEOUT
  176.         for thread in threading.enumerate():
  177.             if thread != threading.currentThread():
  178.                 timeout = endtime - time.time()
  179.                 if timeout <= 0:
  180.                     break
  181.                 thread.join(timeout)
  182.         c = ShutDownResponseCommand(self.daemon)
  183.         c.send(callback=self.response_sent)
  184.         self.daemon.shutdown = True
  185.